<?php
require_once dirname(__FILE__) . '/includes/common.inc.php';
require_once dirname(__FILE__) . '/includes/pdoorm.inc.php';

//header('Content-Type: text/plain');

$tableCustomer = 'ns_v1customer';
$tableCustomerContact = 'ns_CustomerContact';
$tableMyOrder = 'ns_v1myorder';
$tableMyOrderCancel = 'ns_MyOrderCancel';
$tableMyOrderNote = 'ns_MyOrderNote';
$tableMyOrderPackage = 'ns_MyOrderPackage';
$tablePayment = 'ns_v1payment';
$tableExpense = 'ns_Expense';
$tablePackage = 'ns_v1package';
$tableProduct = 'ns_Product';
$tableSample = 'ns_v1sample';
$tablePurchase = 'ns_v1purchase';
$tablePurchaseItem = 'ns_v1purchaseItem';
$tablePurchaseItemSelect = 'ns_v1purchaseItemSelect';
$tableISource = 'ns_ISource';
$tableICulture = 'ns_ICulture';
$tableIReception = 'ns_IReception';
$tableICeremony = 'ns_ICeremony';
$tableUser = 'ns_v1user';


$tableProduce = 'ns_Produce';
$tableProduceEvent = 'ns_ProduceEvent';
$tableProduceItemMail = 'ns_ProduceItemMail';
$tableProduceItemUser = 'ns_ProduceItemUser';
$tableProduceProduct = 'ns_ProduceProduct';
$tableProduceProductSelect = 'ns_ProduceProductSelect';
$tableProducePurchase = 'ns_ProducePurchase';
$tableProducePurchaseItemSelect = 'ns_ProducePurchaseItemSelect';
$tableProducePurchaseItem = 'ns_ProducePurchaseItem';
$tableProduceTrack = 'ns_ProduceTrack';
$tableProduceTransition = 'ns_ProduceTransition';
$tableProduceUser = 'ns_ProduceUser';


try {
  $hostname = HOST_NAME;
  $username = USER_NAME1;
  $password = PASSWORD1;
  $dbname = DATABASE_NAME1;
  $myPdo = new PDO ('mysql:host=' . $hostname, $username, $password);
  $myPdo->exec('USE ' . $dbname);
  $myPdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  
  $username1 = USER_NAME2;
  $password1 = PASSWORD2;
  $dbname1 = DATABASE_NAME2;
  $myPdo1 = new PDO ('mysql:host=' . $hostname, $username1, $password1);
  $myPdo1->exec('USE ' . $dbname1);
  $myPdo1->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
  echo $e->getMessage ();
}

$action = MiscUtils::getParam('action', NULL);
$type = MiscUtils::getParam('type', 1);

try {
  switch ($action) {
    case 'monthly':
      monthly($myPdo,$type);
      break;
      
    default:
      break;
  }
} catch (PDOException $e) {
  echo $e->getMessage();
}


function monthly($myPdo,$type)
{
   global $tableCustomer, $tableCustomerContact, $tableMyOrder, $tableMyOrderCancel, $tablePayment, $tableExpense, $tablePackage, $tableProduct, $tableMyOrderPackage, $tableSample, $tablePurchase, $tableISource, $tableICulture, $tableIReception, $tableICeremony, $tableUser;
	
   $ym = date('Y-m');
   $preym = date('Y-m',strtotime("-1 year"));
   
  //$result = new stdClass();

   
   $sql = "SELECT LEFT(from_unixtime(c.createdDate),7) AS createdDate,ifnull(sum(o.total),0) as total FROM $tableCustomer AS c LEFT JOIN
			(
			SELECT SUM(price) AS total,customerId FROM $tableMyOrder WHERE disposalstatus = 0   GROUP BY customerId) AS o 
			ON c.e_oid = o.customerId
			WHERE c.cancelled = 0 ";
   
   $ymsql = $sql."  AND LEFT(from_unixtime(c.createdDate),7) = '".$ym."' limit 1";
   $preymsql = $sql."  AND LEFT(from_unixtime(c.createdDate),7) = '".$preym."'  limit 1";
   
   $ymdata = getone($ymsql);
   $preymdata = getone($preymsql);
   
   $resultym = $ymdata->total;
   $resultpreym = $preymdata->total;
   $resultdiff = $resultpreym - $resultym;//abs($ymdata->total - $preymdata->total)
   
   
   if($type==1)
   {
      echo "<div style=\"margin:10px 0 0 -8px;font-weight:bold; font-size:12px; font-family: Arial\">$".number_format($resultpreym,2)."</div>";
   }
   

   if($type==2)
   {
      echo "<div style=\"margin:10px 0 0 -8px; font-weight:bold; font-size:12px; font-family: Arial\">$".number_format($resultym,2)."</div>";
   }
   
   if($type==3)
   {
      echo "<div style=\"margin:10px 0 0 -8px;font-weight:bold; font-size:12px; font-family: Arial\">$".number_format($resultdiff,2)."</div>";
   }   
   
   
   //print_r($ymdata);
   //print_r($preymdata);
   //echo json_encode($result);
   
}










?>